<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>php表单post提交</title>
</head>
<body>
	<?php
	// 定义变量并默认设置为空值
	$name=$email=$gender=$comment=$website="";
	$nameErr=$emailErr=$commentErr=$websiteErr="";

	// $_SERVER["REQUEST_METHOD"] 检测表单是否被提交
	if($_SERVER["REQUEST_METHOD"]=="POST"){
		// empty()函数检查$_POST变量是否为空
		if(empty($_POST["name"])){
			{$nameErr="name is required";}
		}else{
			$name=test_input($_POST["name"]);
		}

		if(empty($_POST["email"])){
			{$emailErr="email is required";}
		}else{
			$email=test_input($_POST["email"]);
		}

		if(empty($_POST["comment"])){
			{$commentErr="comment is required";}
		}else{
			$comment=test_input($_POST["comment"]);
		}

		if(empty($_POST['website'])){
			{$websiteErr="website is required";}
		}else{
			$website=test_input($_POST["website"]);
		}
	}

	function test_input($data){
		$data=trim($data); //去除用户输入数据中不必要的字符
		$data=stripslashes($data); //去除用户数据输入中的反斜杠
		$data=htmlspecialchars($data); //把预定义的字符转换为html实体
		return $data;
	}

	?>

	<h2>PHP 表单验证实例</h2>
	<form method="post" action="welcome.php">
		名字：<input type="text" name="name"><span class="error">*<?php echo $nameErr;?></span><br>
		邮箱：<input type="text" name="email"><span class="error">*<?php echo $emailErr;?></span><br>
		网址：<input type="text" name="website"><span class="error">*<?php echo $websiteErr;?></span><br>
		备注：<textarea name="comment" cols="30" rows="10"></textarea><span>*<?php echo $commentErr;?></span><br>
		性别：<input type="radio" name="gender" value="女">女
		<input type="radio" name="gender" value="男" checked="checked">男<br>
		<input type="submit" name="submit">

	</form>

	<?php
	echo "<h2>您输入的内容是：</h2><br>";
	echo $name."<br>";
	echo $email."<br>";
	echo $website."<br>";
	echo $comment."<br>";
	echo $gender;


	?>

</body>
</html>